<?php
class PNTopicsArray extends PNObjectExArray {
  function PNTopicsArray($init=null, $where='')
  {
    $this->PNObject();

    $this->_objType       = 'icarrent_topics';
    $this->_objField      = 'id';
    $this->_objPath       = 'form';
    $this->_objColumnExArray = array( 'is_new'      => "IF ( DATEDIFF(DATE(tpc_cr_date),DATE(NOW())) <1 
                                                             AND
                                                             DATEDIFF(DATE(tpc_cr_date),DATE(NOW())) >-3,
                                                            'T','F')");
/*
    $this->_objColumnExArray = array( 'is_update'      => "IF ( DATEDIFF(DATE(tpc_lu_date),DATE(NOW())) <1,
                                                            'T','F')");

    $this->_objColumnExArray = array( 'is_update'      => "IF ( DATEDIFF(DATE(tpc_lastupdate),DATE(NOW())) <1,
                                                            'T','F')");
*/
/*
    $this->_objColumnExArray = array( );
*/
/*
    $this->_objJoin[]     = array ( 'join_table'  =>  'icarrent_type',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('type_name'),
                            'compare_field_table' =>  'type_id',
                            'compare_field_join'  =>  'id');
*/

    $this->_objJoin[]     = array ( 'join_table'  =>  'icarrent_category',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('category_name'),
                            'compare_field_table' =>  'category_id',
                            'compare_field_join'  =>  'id');

    $this->_objJoin[]     = array ( 'join_table'  =>  'icarrent_subcategory',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('subcategory_name'),
                            'compare_field_table' =>  'subcategory_id',
                            'compare_field_join'  =>  'id');
    $this->_objJoin[]     = array ( 'join_table'  =>  'icarrent_brand',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('brand_name'),
                            'compare_field_table' =>  'brand_id',
                            'compare_field_join'  =>  'id');
    $this->_objJoin[]     = array ( 'join_table'  =>  'icarrent_model',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('model_name'),
                            'compare_field_table' =>  'model_id',
                            'compare_field_join'  =>  'id');
    $this->_objJoin[]     = array ( 'join_table'  =>  'icarrent_shop',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('shop_name'),
                            'compare_field_table' =>  'shop_id',
                            'compare_field_join'  =>  'id');

    $this->_init($init, $where);
  }
  
  function genFilter(){
    $shop_id  = FormUtil::getPassedValue ('shop_id', false);

    $wheres = array();

    //filter by selector
    if ($shop_id && intval($shop_id)) $wheres[] = "tpc_shop_id ='$shop_id'";

    return implode(' AND ', $wheres) ;
  }




/*  
  function genSort(){
    $sort  = FormUtil::getPassedValue ('order', false);
    $result = false;
    switch ($sort){
      case 'id':
        $result = 'ORDER BY tpc_id DESC';
        break;
      case 'type':
        $result = 'ORDER BY tpc_type_id';
        break;
      case 'category':
        $result = 'ORDER BY tpc_category_id';
        break;
      case 'subcategory':
        $result = 'ORDER BY tpc_subcategory_id';
        break;
      case 'title':
        $result = 'ORDER BY tpc_title';
        break;
      case 'date':
        $result = 'ORDER BY tpc_cr_date DESC';
        break;
      case 'view':
        $result = 'ORDER BY tpc_count_view DESC';
        break;
      case 'post':
        $result = 'ORDER BY count_post DESC';
        break;
    }
    if ($result){
      return $result;
    }else{
      //default order
      return 'ORDER BY tpc_cr_date DESC';
    }
  
  }



  function genFilter(){
    $filter  = FormUtil::getPassedValue ('filter', false, 'REQUEST');
    $keyword = DataUtil::formatForStore($filter['keyword']);
    $type = DataUtil::formatForStore($filter['type']);
    $category_id = DataUtil::formatForStore(FormUtil::getPassedValue ('category_id', $filter['category_id']));
    $subcategory_id = DataUtil::formatForStore(FormUtil::getPassedValue ('subcategory_id', $filter['subcategory_id']));
    $brand_id = DataUtil::formatForStore(FormUtil::getPassedValue ('brand_id', false));
    $carproperty_id = DataUtil::formatForStore(FormUtil::getPassedValue ('carproperty_id', false));
    $model_id = DataUtil::formatForStore(FormUtil::getPassedValue ('model_id', false));


    $type_id = DataUtil::formatForStore(FormUtil::getPassedValue ('type_id',$filter['type_id']));

    $wheres = array();
    //filter by user_id
    $user_id = DataUtil::formatForStore(FormUtil::getPassedValue ('user_id', false));
    if ($user_id) $wheres[] = "tpc_cr_uid = '$user_id'";
    //filter by keyword
    if ($keyword && $type){
      switch($type){
        case 'id': $wheres[] = "tpc_ref_id LIKE '%". $keyword ."%'"; break;
        case 'name': $wheres[] = "tpc_name LIKE '%". $keyword ."%'"; break;
        case 'brand': $wheres[] = "tpc_car_brand LIKE '%". $keyword ."%'"; break;
        case 'title': $wheres[] = "tpc_title LIKE '%". $keyword ."%'"; break;
        case 'category_name': $wheres[] = "cty_name LIKE '%". $keyword ."%'"; break;
        case 'subcategory_name': $wheres[] = "scy_name LIKE '%". $keyword ."%'"; break;
      }
    }

    //filter by selector 
    if($category_id && intval($category_id) && $subcategory_id && intval($subcategory_id) && $brand_id && intval($brand_id) && $model_id && intval($model_id)){
      $wheres[] = "tpc_category_id ='$category_id' AND tpc_subcategory_id ='$subcategory_id' AND tpc_brand_id ='$brand_id' AND tpc_model_id ='$model_id' ";
    }else if($category_id && intval($category_id) && $subcategory_id && intval($subcategory_id) && $brand_id && intval($brand_id)){
      $wheres[] = "tpc_category_id ='$category_id' AND tpc_subcategory_id ='$subcategory_id' AND tpc_brand_id ='$brand_id'";
    }else if($category_id && intval($category_id) && $subcategory_id && intval($subcategory_id) && $carproperty_id && intval($carproperty_id)){
      $wheres[] = "tpc_category_id ='$category_id' AND tpc_subcategory_id ='$subcategory_id' AND tpc_carproperty_id LIKE '%$carproperty_id%'";
      //var_dump($wheres); exit;
    }else if($category_id && intval($category_id) && $subcategory_id && intval($subcategory_id)){
      $wheres[] = "tpc_category_id ='$category_id' AND tpc_subcategory_id ='$subcategory_id'";
    }else if($category_id && intval($category_id)){
      $wheres[] = "tpc_category_id ='$category_id'";
    }else if($subcategory_id && intval($subcategory_id)){
      $wheres[] = "tpc_subcategory_id ='$subcategory_id'";
    }

    if ($type_id && intval($type_id)) $wheres[] = "tpc_type_id ='$type_id'";
  
    //not display admin
    $pagetype = InnoUtil::getPageType();
    if ($pagetype == 'home'){
      $admin_id = constant('ADMIN_UID');
      $wheres[] = "(tpc_cr_uid <> '$admin_id' OR tpc_obj_status = '1')";
    }

    return implode(' AND ', $wheres) ;
  }

  function selectExtendResult(){
    $user_id = DataUtil::formatForStore(FormUtil::getPassedValue ('user_id', false));
    $result = array();
    //get user info
    if ($user_id){
      $result['user'] = InnoUtil::getUserInfo($user_id);
    }

    $category_id = DataUtil::formatForStore(FormUtil::getPassedValue ('category_id', false));
    $subcategory_id = DataUtil::formatForStore(FormUtil::getPassedValue ('subcategory_id', false));
    $brand_id = DataUtil::formatForStore(FormUtil::getPassedValue ('brand_id', false));


    $result['category'] = DBUtil::selectObjectArray ('icarrent_category');
   
    if($category_id && $subcategory_id && $brand_id){
      //Filter model
      $pntables = pnDBGetTables();
      $tableModel  = $pntables['icarrent_model'];
      $columnModel = $pntables['icarrent_model_column'];

      $tableSubCategoryModel  = $pntables['icarrent_subcategory_model'];
      $columnSubCategoryModel = $pntables['icarrent_subcategory_model_column'];

      $sql = "SELECT
                $tableModel.$columnModel[id],
                $tableModel.$columnModel[name]
              FROM
                $tableSubCategoryModel, $tableModel
              WHERE 
                $tableSubCategoryModel.$columnSubCategoryModel[category_id] = $category_id
              AND 
                $tableSubCategoryModel.$columnSubCategoryModel[subcategory_id] = $subcategory_id
              AND 
                $tableSubCategoryModel.$columnSubCategoryModel[brand_id] = $brand_id
              AND 
                $tableSubCategoryModel.$columnSubCategoryModel[model_id] = $tableModel.$columnModel[id]
              ORDER BY 
                $tableModel.$columnModel[name] ASC ";
      
      $column = array("id", "name");
      $sqlResult = DBUtil::executeSQL($sql);
      $result['modelArray'] = DBUtil::marshallObjects ($sqlResult, $column);

    }else if($category_id && $subcategory_id){

      $pntables = pnDBGetTables();

      //Filter brand
      $tableBrand  = $pntables['icarrent_brand'];
      $columnBrand = $pntables['icarrent_brand_column'];

      $tableSubCategoryBrand  = $pntables['icarrent_subcategory_brand'];
      $columnSubCategoryBrand = $pntables['icarrent_subcategory_brand_column'];

      $sql = "SELECT
                $tableBrand.$columnBrand[id],
                $tableBrand.$columnBrand[name]
              FROM
                $tableSubCategoryBrand, $tableBrand
              WHERE 
                $tableSubCategoryBrand.$columnSubCategoryBrand[category_id] = $category_id
              AND 
                $tableSubCategoryBrand.$columnSubCategoryBrand[subcategory_id] = $subcategory_id
              AND 
                $tableSubCategoryBrand.$columnSubCategoryBrand[brand_id] = $tableBrand.$columnBrand[id]
              ORDER BY 
                $tableBrand.$columnBrand[name] ASC ";
      
      $column = array("id", "name");
      $sqlResult = DBUtil::executeSQL($sql);
      $result['brandArray'] = DBUtil::marshallObjects ($sqlResult, $column);


      //Filter Car properties
      $tableCarProperty  = $pntables['icarrent_carproperty'];
      $columnCarProperty = $pntables['icarrent_carproperty_column'];

      $tableSubCategoryCarProperty  = $pntables['icarrent_subcategory_carproperty'];
      $columnSubCategoryCarProperty = $pntables['icarrent_subcategory_carproperty_column'];

      $sql = "SELECT
                $tableCarProperty.$columnCarProperty[id],
                $tableCarProperty.$columnCarProperty[name]
              FROM
                $tableSubCategoryCarProperty, $tableCarProperty
              WHERE 
                $tableSubCategoryCarProperty.$columnSubCategoryCarProperty[category_id] = $category_id
              AND 
                $tableSubCategoryCarProperty.$columnSubCategoryCarProperty[subcategory_id] = $subcategory_id
              AND 
                $tableSubCategoryCarProperty.$columnSubCategoryCarProperty[carproperty_id] = $tableCarProperty.$columnCarProperty[id]
              ORDER BY 
                $tableCarProperty.$columnCarProperty[name] ASC ";
      
      $column = array("id", "name");
      $sqlResult = DBUtil::executeSQL($sql);
      $result['carPropertiesArray'] = DBUtil::marshallObjects ($sqlResult, $column);

      //var_dump($result); exit;
    }else if($category_id){
      $result['category_name'] = DBUtil::selectFieldByID ('icarrent_category', 'name', $category_id);
      $result['subcategory'] = DBUtil::selectObjectArray ('icarrent_subcategory', "WHERE scy_category_id = '$category_id' ", "scy_name");
    }

    if($subcategory_id){
      $result['subcategory_name'] = DBUtil::selectFieldByID ('icarrent_subcategory', 'name', $subcategory_id);
    }
    

    $result['productStateArray'] = DBUtil::selectObjectArray('icarrent_productstate', '', '', -1, -1, '', NULL, NULL,array("id","name"));


    return $result;
  }
  
  */
/*
  function selectPostProcess(){
    $id = $this->_objData['id'];

    $result['fimage'] = DBUtil::selectObjectArray('icarrent_resource_image',
                                                   "WHERE rsi_referer_id = ".$this->_objData['id']." and rsi_status = 1",
                                                    "rsi_id",
                                                    -1,
                                                    1,
                                                    '',
                                                    null,
                                                    array("filename","thumbname")
                                                  );

    //return $id;
  }
*/

}